<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ExcelJS 示例</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/exceljs/4.4.0/exceljs.js"></script>
</head>
<body>
    <h1>ExcelJS 示例</h1>
    <button id="exportButton">导出 Excel 文件</button>
    <input type="file" id="importFile" accept=".xlsx,.xls" />
    <div id="output"></div>

    <script>
        // 导出 Excel 文件
        document.getElementById("exportButton").addEventListener("click", () => {
            // 创建工作簿
            const workbook = new ExcelJS.Workbook();

            // 添加工作表
            const worksheet = workbook.addWorksheet('Sheet1');

            // 添加表头
            worksheet.columns = [
            { header: 'Name', key: 'name', width: 10 },
            { header: 'Age', key: 'age', width: 10 },
            { header: 'City', key: 'city', width: 15 }
            ];

            // 添加数据行
            worksheet.addRow({ name: 'Alice', age: 25, city: 'New York' });
            worksheet.addRow({ name: 'Bob', age: 30, city: 'Los Angeles' });

            // 导出 Excel 文件
            workbook.xlsx.writeBuffer().then(buffer => {
            const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'example.xlsx';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
            });
        });

    </script>
</body>
</html>